﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KhaoSatSinhVienTotNghiep.Model;
using System.Web.Helpers;

namespace KhaoSatSinhVienTotNghiep.Web.Controllers
{
    public class CauHoiController : Controller
    {
        /// <summary>
        /// GetAll
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var data = CauHoi.GetAll();
            return View(data);
        }


        [HttpGet]
        public ActionResult ThemMoi()
        {
            return View();
        }


        [HttpPost]
        public ActionResult ThemMoi(CauHoiModel model, FormCollection form)
        {
            bool status = false;
            if (!ModelState.IsValid)
                return View(model);
            var ch = new CauHoi
            {
                ChiTiet = model.NoiDung,
                MaLoaiKS = model.MaLoaiKS,
                MaKieu = model.MaKieuTL
            };
            if (CauHoi.AddNew(ch))
            {
                if (model.MaKieuTL == "Loai3")
                    status = true;
                //Thêm câu trả lời cho câu hỏi
                int soLuongCauTraLoi = int.Parse(form["SoLuongCauTraLoi"]);
                string nameCauTraLoi = "CauTraLoi";

                //Tạo câu trả lời
                for (int i = 1; i <= soLuongCauTraLoi; i++)
                {
                   
                    var chiTiet = form[nameCauTraLoi + i];
                    if (@chiTiet == "" || @chiTiet == "Khác")
                        status = true;
                    CauTL cauTraLoi = new CauTL
                    {
                        ChiTiet = chiTiet,
                        MaCauHoi = ch.MaCauHoi,
                        Status = status
                    };
                    CauTL.AddNew(cauTraLoi);
                }

                ViewBag.Message = "Thêm thành công";
                return RedirectToAction("Index");
            }

            ViewBag.Message = "Thêm thất bại";
            return View(model);
        }



        [HttpGet]

        public ActionResult CapNhat(int id)
        {
            var cauhoi = CauHoi.GetByID(id);
            if (cauhoi == null)
            {
                ViewBag.Message = "Không tìm thấy câu hỏi";
                return View();
            }
            return View(new CauHoiModel
            {
                NoiDung = cauhoi.ChiTiet,
                MaCauHoi = cauhoi.MaCauHoi,
                MaKieuTL=cauhoi.MaKieu,
                MaLoaiKS=cauhoi.MaLoaiKS
            });
        }


        [HttpPost]
        public ActionResult CapNhat(CauHoiModel model,FormCollection form)
        {
            var cauhoi = CauHoi.GetByID(model.MaCauHoi);
            cauhoi.ChiTiet = model.NoiDung;
            cauhoi.MaKieu = model.MaKieuTL;
            cauhoi.MaLoaiKS = model.MaLoaiKS;

            if (CauHoi.Update(cauhoi))
            {
                var cauTL = CauTL.GetByIDCauHoi(model.MaCauHoi);
                foreach (var item in cauTL)
                {
                   
                }
            }
            return RedirectToAction("Index");
        }



        public ActionResult Xoa(int id)
        {
            List<CauTL> cautl = CauTL.GetByIDCauHoi(id);
            foreach (var item in cautl)
            {
                CauTL.DeleteCauTL(item.MaCauTL);
            }
            CauHoi.Delete(id);
            return RedirectToAction("Index");
        }

        public ActionResult GetByID(int id)
        {
            CauHoi.GetByID(id);
            return RedirectToAction("Index");
        }
        public JsonResult CauHoiDaTonTai(string noidung)
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }


        public PartialViewResult GetInputQuestion(int number)
        {
            return PartialView(number);
        }

        [HttpGet]
        public ViewResult LayCauHoiTheoLoaiKS()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LayCauHoiTheoLoaiKS(FormCollection frm)
        {
            string malks = frm["LoaiKS"];
            List<CauHoi> list = CauHoi.LayCauHoiTheoLoaiKS(malks);
            return View(list);
        }


      
        
    }
}
